<template>
  <a-row :gutter="24">
    <a-col :md="24">
      <a-card :style="cardStyle" :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">

              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('search.outBillNo')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-input
                    v-model="queryParam.outBillNumber"></a-input>
                </a-form-item>
              </a-col>
              <!--              <a-col :md="6" :sm="24">-->
              <!--                <a-form-item :label="$t('search.chequeNumber')" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
              <!--                  <a-input-->
              <!--                    v-model="queryParam.receiptNumber"></a-input>-->
              <!--                </a-form-item>-->
              <!--              </a-col>-->
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('search.billCreateTimeRange')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-range-picker
                    style="width: 100%"
                    v-model="queryParam.createTimeRange"
                    format="YYYY-MM-DD"
                    :placeholder="[$t('placeholder.pleaseInputStartTime'), $t('placeholder.pleaseInputEndTime')]"
                    @change="onDateChange"
                  />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('search.customer')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select :placeholder="$t('placeholder.pleaseSelectCustomer')" v-model="queryParam.supplier"
                            :dropdownMatchSelectWidth="false" showSearch allow-clear optionFilterProp="children">
                    <a-select-option v-for="(item,index) in cusList" :key="index" :value="item.id">
                      {{ item.supplier }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                  <a-button type="primary" @click="searchQuery">{{ $t('button.search') }}</a-button>
                  <!--                  <a-button style="margin-left: 8px" v-print="'#reportPrint'"-->
                  <!--                            icon="printer">{{ $t('button.print') }}</a-button>-->
                  <!--                  <a-button style="margin-left: 8px" @click="exportExcel"-->
                  <!--                            icon="download">{{ $t('button.export') }}</a-button>-->
                </span>
              </a-col>
              <template v-if="toggleSearchStatus">
                <a-col :md="6" :sm="24">
                  <a-form-item :label="$t('search.salesMan')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <a-select :placeholder="$t('placeholder.pleaseSelectSalesMan')" v-model="queryParam.salesMan"
                              :dropdownMatchSelectWidth="false" showSearch allow-clear optionFilterProp="children">
                      <a-select-option v-for="(item,index) in userList" :key="index" :value="item.id">
                        {{ item.userName }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
              </template>
            </a-row>
          </a-form>
        </div>
        <!-- table区域-begin -->
        <section ref="print" id="reportPrint">
          <a-table
            bordered
            ref="table"
            size="middle"
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            @change="handleTableChange">

          </a-table>
        </section>
      </a-card>
    </a-col>
  </a-row>
</template>

<script>
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import {getFormatDate, getPrevMonthFormatDate} from '@/utils/util'
import {findBySelectCus, getUserList} from '@/api/api'
import moment from 'moment'
import {getAction} from "@api/manage";
import dayjs from "dayjs";

export default {
  name: "ChequeReport",
  mixins: [JeecgListMixin],
  data() {
    return {
      labelCol: {
        span: 5
      },
      wrapperCol: {
        span: 18,
        offset: 1
      },
      queryParam: {
        chequeNumber: '',
        beginTime: getPrevMonthFormatDate(3),
        endTime: getFormatDate(),
        createTimeRange: [moment(getPrevMonthFormatDate(3)), moment(getFormatDate())],
        organId: '',
        salesMan: ''
      },
      ipagination: {
        pageSize: 10
      },
      cusList: [],
      userList: [],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: 'rowIndex',
          width: 40,
          align: "center",
          customRender: (t, r, index) => parseInt(index) + 1
        },
        {
          title: this.$t('search.outBillNo'),
          dataIndex: 'billNumber',
          width: 120
        },
        {
          title: this.$t('search.outBillNo'),
          dataIndex: 'receiptNumber',
          width: 120
        },
        {
          title: this.$t('search.customer'),
          dataIndex: 'customerName',
          width: 100
        },
        {
          title: this.$t('search.materialParam'),
          dataIndex: 'materialInfo',
          width: 100
        },
        {
          title: this.$t('search.salesMan'),
          dataIndex: 'salesMan',
          width: 100
        },
        {
          title: this.$t('search.billAmount'),
          dataIndex: 'amount',
          width: 90
        },
        {
          title: this.$t('search.outDate'),
          dataIndex: 'deliveryDate',
          width: 100
        },
        {
          title: this.$t('search.signTime'),
          dataIndex: 'signDate',
          width: 100
        },
      ],
      url: {
        list: "/accountHead/cash/report"
      }
    }
  },
  created() {
    this.initCustomer()
    //this.initUser()
  },
  methods: {
    moment,
    initCustomer() {
      findBySelectCus({}).then((res) => {
        if (res) {
          this.cusList = res
        }
      })
    },
    initUser() {
      getUserList({}).then((res) => {
        if (res) {
          this.userList = res
        }
      })
    },
    loadData(arg) {
      if (arg === 1) {
        this.ipagination.current = 1
      }
      let params = this.getQueryParams()
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.code === 200) {
          this.dataSource = res.data.rows
          this.ipagination.total = res.data.total
        } else {
          this.$message.warning(res.message)
        }
      }).catch((error) => {
        this.$message.error(this.$t('message.loadDataFailed'))
      }).finally(() => {
        this.loading = false
      })
    },
    getQueryParams() {
      let param = Object.assign({}, this.queryParam)
      if (this.queryParam.createTimeRange && this.queryParam.createTimeRange.length === 2) {
        param.beginTime = dayjs(this.queryParam.createTimeRange[0]).format('YYYY-MM-DD')
        param.endTime = dayjs(this.queryParam.createTimeRange[1]).format('YYYY-MM-DD')
      }
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize
      return param
    },
    onDateChange: function (value, dateString) {
      this.queryParam.beginTime = dateString[0]
      this.queryParam.endTime = dateString[1]
      if (dateString[0] && dateString[1]) {
        this.queryParam.createTimeRange = [moment(dateString[0]), moment(dateString[1])]
      }
    },
    searchQuery() {
      if (this.queryParam.beginTime == '' || this.queryParam.endTime == '') {
        this.$message.warning(this.$t('placeholder.pleaseSelectBillDate'))
      } else {
        this.loadData(1)
      }
    },
    exportExcel() {
      let list = []
      let head = '出库单号,客户,商品信息,销售员,订单金额,发货日期,签收日期'
      for (let i = 0; i < this.dataSource.length; i++) {
        let item = []
        let ds = this.dataSource[i]
        item.push(ds.number, ds.customerName, ds.materialInfo, ds.salesMan,
          ds.chequeAmount, ds.deliveryDate, ds.signDate)
        list.push(item)
      }
      let tip = '单据日期：' + this.queryParam.beginTime + '~' + this.queryParam.endTime
      this.handleExportXlsPost('支票汇总', '支票汇总', head, tip, list)
    }
  }
}
</script>

<style scoped>
@import '~@assets/less/common.less';
</style>